package com.yxx.data.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.data.domain.Demographic;
import com.yxx.data.service.DemographicService;
import com.yxx.data.vo.DemographicVo;
import com.yxx.sys.common.AppSecureUtils;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-06-11
 */
@RestController
@RequestMapping("/demographic")
@Api("人口统计学信息控制器")
public class DemographicController {

  @Autowired
  DemographicService demographicService;

  @GetMapping("/loadAllInformation")
  @ApiOperation("查询人口统计学信息")
  public DataGridView loadAllInformation(DemographicVo demographicVo) {
    IPage<Demographic> page = new Page<Demographic>(demographicVo.getPage(), demographicVo.getLimit());
    QueryWrapper<Demographic> queryWrapper = new QueryWrapper<Demographic>();

    queryWrapper.eq(StrUtil.isNotBlank(demographicVo.getHospitalId()), "hospital_id", demographicVo.getHospitalId());
    queryWrapper.like(StrUtil.isNotBlank(demographicVo.getNativePlace()), "native_place", demographicVo.getNativePlace());
    queryWrapper.eq(demographicVo.getHospital() != null && demographicVo.getHospital() != -1, "hospital", demographicVo.getHospital());
    queryWrapper.orderByDesc("id");
    demographicService.page(page, queryWrapper);
    return new DataGridView(page.getTotal(), page.getRecords());
  }

  @PostMapping("/addInformation")
  @ApiOperation("添加人口统计学信息")
  public Result addInformation(DemographicVo demographicVo) {
    demographicVo.setName(AppSecureUtils.getEncryptData(demographicVo.getName()));

    demographicVo.setCreatedTime(LocalDate.now());
    demographicVo.setChangeTime(LocalDate.now());
    boolean succ = demographicService.save(demographicVo);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/updateInformation")
  @ApiOperation("更新人口统计学信息")
  public Result updateInformation(DemographicVo demographicVo) {
    try {
      boolean b = demographicService.updateById(demographicVo);
      if(!b) return Result.UPDATE_REEOR;
      return Result.UPDATE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.UPDATE_REEOR;
    }
  }

  @PostMapping("/deleteInformation")
  @ApiOperation("删除人口统计学信息")
  public Result deleteInformation(Integer id) {
    try {
      demographicService.removeById(id);
      return Result.DELETE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.DELETE_REEOR;
    }
  }

}
